#pragma once

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QVector>
#include <QWidget>

class QListWidget;
class QAbstractItemDelegate;
class KeySearchWidget;
class KeySearchResultDlg : public QWidget {
  Q_OBJECT
 public:
  KeySearchResultDlg(KeySearchWidget* parent, int itemHeight);

  void setListItemDelegate(QAbstractItemDelegate* itemDelegate);
  void setMaxHeight(int h);
  void setWidth(int w);
  void setAutoSelect(bool bAutoSelect) { m_bAutoSelect = bAutoSelect; };
  void setAutoKillFocus(bool bAutoKillFocus) {
    m_bAutoKillFocus = bAutoKillFocus;
  };

 public slots:
  void onResultReached(QJsonArray data);

 protected:
  void paintEvent(QPaintEvent* event) override;

  void showEvent(QShowEvent* event) override;
  void hideEvent(QHideEvent* event) override;
  bool eventFilter(QObject* watched, QEvent* event);

 private:
  void selectNextItem(int index = -1);
  void selectPreviousItem(int index);

 private:
  KeySearchWidget* m_host = nullptr;
  QListWidget* m_list;
  QVector<int> m_columnWidths;

  int m_itemHeight = 0;
  bool m_bAutoSelect = true;
  bool m_bAutoKillFocus = true;
};